package org.taobao.ibatiside.ui.action;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.lang.StringUtils;
import org.eclipse.datatools.sqltools.core.profile.ProfileUtil;
import org.eclipse.datatools.sqltools.editor.core.connection.ISQLEditorConnectionInfo;
import org.eclipse.datatools.sqltools.sqleditor.SQLEditor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.taobao.ibatiside.ui.Activator;
import org.taobao.ibatiside.ui.util.MiscHelper;
import org.taobao.ibatiside.ui.wizard.SqlEditorWizard;

public class SqlEditorNewStmtAction extends SqlEditorAction {
	public static final String ID="org.taobao.ibatiside.ui.action.sqleditor.newstmt";
	public SqlEditorNewStmtAction() {
		super();
		setId(ID);
		setText("New Statement");
		setImageDescriptor(Activator.getIcon("op16/statement.png"));
		setDescription("Create a new statement base on what you selected");
	}
	@Override
	public void run() {
		SQLEditor editor = getEditor();
		if (editor == null) return;
		ISQLEditorConnectionInfo conn_info = editor.getConnectionInfo();
		if (conn_info == null || conn_info.getConnectionProfile() == null || conn_info.getDatabaseName() == null){
			MessageDialog.openError(editor.getEditorSite().getShell(), "ERROR", "YOU ARE NOT WORKING ON A CONNECTIOON, PLEASE SELECT ONE!");
			return;
		}
		String sql = editor.getSelectedText();
		
		Connection test_connection = ProfileUtil.createConnection(conn_info.getConnectionProfile(), conn_info.getDatabaseName());
		if (test_connection == null){
			MessageDialog.openError(editor.getEditorSite().getShell(), "ERROR","CAN'T ESTABLISH CONNECTION TO '"+conn_info.getConnectionProfileName()+"'");
			return;
		}
		try {
			validateSQL(test_connection, sql);
		} catch (SQLException e) {
			MessageDialog.openError(editor.getEditorSite().getShell(), "ERROR", e.getLocalizedMessage());
			return;
		}
		SqlEditorWizard wizard = new SqlEditorWizard();
		wizard.getModel().setOpSql(sql);
		wizard.getModel().setConnectionProfile(conn_info.getConnectionProfile());
		wizard.getModel().setDtpStmt(MiscHelper.parseQueryStatement(wizard.getModel()));
		wizard.setWindowTitle("Create Statement from SQL editor!");
		WizardDialog dialog = new WizardDialog(editor.getEditorSite().getShell(), wizard);
		dialog.setPageSize(700, 600);
		if (dialog.open()== WizardDialog.OK){
			postWizard(wizard);
		}
	}
	private void postWizard(SqlEditorWizard wizard) {
		
	}
	private void validateSQL(Connection test_connection, String sql) throws SQLException {
		if (StringUtils.isNotBlank(sql)){
			Statement stmt = null;
			try {
				test_connection.setAutoCommit(false);
				stmt = test_connection.createStatement();
				stmt.execute(sql);
				test_connection.commit();
				test_connection.rollback();
			}finally{
				test_connection.setAutoCommit(true);
				if (stmt != null){
					stmt.close();
				}
			}
		}else{
			throw new SQLException("Empty Statement!");
		}
	}

}
